package com.jprocms.module.cms.controller.admin.content;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.jprocms.module.cms.controller.admin.content.relatevo.ContentRelateBatchCreateReqVO;
import com.jprocms.module.cms.controller.admin.content.relatevo.ContentRelateReqVO;
import com.jprocms.module.cms.controller.admin.content.relatevo.ContentRelateRespVO;
import com.jprocms.module.cms.controller.admin.content.relatevo.ContentRelateSortVO;
import com.jprocms.module.cms.convert.content.ContentRelateConvert;
import com.jprocms.module.cms.dal.dataobject.content.ContentRelateDO;
import com.jprocms.module.cms.service.content.ContentRelateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.jpro.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 相关内容")
@RestController
@RequestMapping("/cms/content/relate")
@Validated
public class ContentRelateController {

    @Resource
    private ContentRelateService contentRelateService;

    @PostMapping("/create")
    @Operation(summary = "创建相关内容")
    @PreAuthorize("@ss.hasPermission('cms:content-relate:create')")
    public CommonResult<Boolean> createContentRelate(@Valid @RequestBody ContentRelateBatchCreateReqVO createReqVO) {
        contentRelateService.createContentRelate(createReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除相关内容")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:content-relate:create')")
    public CommonResult<Boolean> deleteContentRelate(@RequestParam("id") Long id) {
        contentRelateService.deleteContentRelate(id);
        return success(true);
    }

    @PostMapping("/sort")
    @Operation(summary = "相关内容排序")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:content-relate:create')")
    public CommonResult<Boolean> sortContentRelate(@Valid @RequestBody ContentRelateSortVO sortVO) {
        contentRelateService.sortContentRelate(sortVO);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得相关内容")
    public CommonResult<PageResult<ContentRelateRespVO>> getContentRelatePage(@Valid ContentRelateReqVO pageVO) {
        PageResult<ContentRelateDO> relateDOS = contentRelateService.getContentRelateList(pageVO);
        return success(ContentRelateConvert.INSTANCE.convertPage(relateDOS));
    }


}
